﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using projectManagementWeb.Validations;
using System.Web.Security;


namespace projectManagementWeb.Models.ProjectLeader
{

    [ModuleClassValidationAttribute]
    public class createModuleModel
    {
        projectmanagementDBDataContext _dbContext = null;
        public createModuleModel()
        {
            _dbContext = new projectmanagementDBDataContext();
        }

        [Required(ErrorMessage = "El {0} es requerido")]
        [Display(Name = "Proyecto")]
        public int projectID { get; set; }


        [Required(ErrorMessage = "El {0} es requerido")]
        [StringLength(100, ErrorMessage = "El {0} debe contener por lo menos un carater y ser menor a 100 caracteres.", MinimumLength = 1)]
        [Display(Name = "Nombre del Módulo")]
        public string Name { get; set; }


        [Required(ErrorMessage = "La {0} es requerida")]
        [StringLength(100, ErrorMessage = "La {0} debe contener por lo menos un carater y ser menor a 100 caracteres.", MinimumLength = 1)]
        [Display(Name = "Descripción del Módulo")]
        public string Description { get; set; }

        [Required(ErrorMessage = "La {0} es requerida")]
        //[DataType(DataType.DateTime)]
        [Display(Name = "Fecha de Inicio del Módulo")]
        public DateTime startDate { get; set; }

        [Required(ErrorMessage = "La {0} es requerida")]
        [Display(Name = "Fecha de Finalización del Módulo")]
        public DateTime endDate { get; set; }

        
        public int userID { get; set; }

        public IEnumerable<SelectListItem> ProjectIDList
        {
            get
            {
                IEnumerable<SelectListItem> projectIDList = null;

                if (Roles.IsUserInRole("Administrador del Sistema"))
                {
                    projectIDList = _dbContext.spGetAllProjects()
                    .Select(i => new SelectListItem
                    {
                        Value = i.ID.ToString(),
                        Text = i.name.ToString()
                    }).ToList();
                }
                else
                {
                    projectIDList = _dbContext.spGetProjectsByProjectLeaderID(userID)
                    .Select(i => new SelectListItem
                    {
                        Value = i.ID.ToString(),
                        Text = i.name.ToString()
                    }).ToList();
                }


                if (projectIDList == null || projectIDList.Count() == 0)
                {
                    List < SelectListItem > list = new List<SelectListItem>();
                    list.Add(new SelectListItem
                    {
                        Value = "-1",
                        Text = "No tiene proyectos válidos"
                    });
                    return list;
                }
                return projectIDList;
            }
        }

    }
     

}
